<?php

namespace App\Http\Controllers;


use App\Repos\MemberRepo;

class UserController extends ApiController
{
    protected $memberRepo;
    
    public function __construct(MemberRepo $memberRepo)
    {
        parent::__construct();
        $this->memberRepo = $memberRepo;
    }
    
    public function strangers()
    {
        $list = $this->memberRepo->find($this->user)->randomStrangerUsers($this->user->id,5);
        
        return $this->setPage(0)->success($list);
    }
    
    //使用金币解锁等级
    public function upgradeLevel()
    {
        $level_gold = config('car.levels');
        $user_level = $this->user->getUserLevel();
        
        if($user_level >= \count(config('car.levels'))){
            return $this->error(1,'已达最大值');
        }
        //此等级存在
        if(array_key_exists($user_level,$level_gold)){
            $gold_need = $level_gold[$user_level+1]['gold'];
            //检查用户余额是否足够
            if($this->user->getCredit() < $gold_need){
                return $this->error(1,'用户余额不足以解锁,你需要至少'.$gold_need);
            }
            $this->memberRepo::decrementUserCredit($this->user,$gold_need);
            $this->user->increment('level');
            
            return $this->success('您已成功解锁');
        }
    
        return $this->error(1,'解锁失败，请重试');
        
    }
    
}
